package com.example.demo.util;

import java.util.Base64;

/**
 * base64 加密解密
 * 最优编码方式工具
 */
public class Base64UtilXb {


    public static void main(String[] args) {
        System.out.println(encrypt("我爱编码！"));
    }

    /**
     * BASE64加密
     */
    public static String encrypt(String str) {
        if (str == null) return null;
        byte[] bytes = str.getBytes();
        //Base64 加密
        String encoded = Base64.getEncoder().encodeToString(bytes);
        System.out.println("Base 64 加密后：" + encoded);
        return encoded;
    }

    /**
     * BASE64解密
     *
     * @throws Exception
     */
    public static String decrypt(String key) {
        if (key == null) return null;
        byte[] decoded = Base64.getDecoder().decode(key);
        String decodeStr = new String(decoded);
        System.out.println("Base 64 解密后：" + decodeStr);
        return decodeStr;
    }

}